package it.av.training.rest.exception;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * Utility class that logs {@link RestException} in a uniform way
 * 
 * @author alessandro vincelli
 * 
 */
public class ExceptionLogger {

    /**
     * Logs the message of the exception to the error log, and the message and stacktrace to the errordetail log
     * 
     * @param e
     *            The RestException to log
     * @param className
     *            The class name where the exception was thrown
     * @param severity
     *            The level to use when logging
     */
    public static void log(RestException e, String className, Level severity) {
        String logStatement = createLogMessage(e);
        Logger logError = Logger.getLogger(Constants._LOG_ERROR_NAME + className);
        Logger logErrorDetail = Logger.getLogger(Constants._LOG_ERROR_DETAILS_NAME + className);
        logError.log(severity, logStatement);
        logErrorDetail.log(severity, logStatement, e);
    }

    private static String createLogMessage(RestException e) {
        return "[guid=" + e.getGuid() + ", errorCode=" + e.getErrorCode().getCode() + ", message=" + e.getMessage() + "]";
    }

}
